#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
  ////输入重定向
  ////close(0);
  //int fd = open("txt", O_RDONLY);
  //dup2(fd, 0);
  ////close(fd);
  //char outBuff[1024] = { 0 };
  //ssize_t s = read(0, outBuff, sizeof(outBuff));
  //outBuff[s] = '\0';
  //close(fd);
  //printf("%s", outBuff);

  //输出重定向
  //close(1);
  //int fd = open("txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
  //追加重定向
  int fd = open("txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
  if(fd < 0) { perror("open err"); exit(1); }
  dup2(fd, 1);
  const char* inBuff = "hello redirect!\n";
  //printf("%s", inBuff);
  //fprintf(stdout, "%s", inBuff);
  write(1, inBuff, strlen(inBuff));
  close(fd);
  return 0;
}
